{% extends "base.html" %}

{% block title %}系统日志 - 协同调度信息系统{% endblock %}

{% block content %}
<div class="row">
    <div class="col-12">
        <div class="card">
            <div class="card-header">
                <h4 class="mb-0">
                    <i class="fas fa-list"></i> 系统日志
                </h4>
            </div>
            <div class="card-body">
                {% if logs.items %}
                <div class="table-responsive">
                    <table class="table table-hover">
                        <thead class="table-dark">
                            <tr>
                                <th>时间</th>
                                <th>级别</th>
                                <th>操作类型</th>
                                <th>消息</th>
                                <th>用户</th>
                            </tr>
                        </thead>
                        <tbody>
                            {% for log in logs.items %}
                            <tr>
                                <td>{{ log.timestamp.strftime('%Y-%m-%d %H:%M:%S') }}</td>
                                <td>
                                    {% if log.level == 'info' %}
                                        <span class="badge bg-info">信息</span>
                                    {% elif log.level == 'warning' %}
                                        <span class="badge bg-warning text-dark">警告</span>
                                    {% elif log.level == 'error' %}
                                        <span class="badge bg-danger">错误</span>
                                    {% elif log.level == 'debug' %}
                                        <span class="badge bg-secondary">调试</span>
                                    {% else %}
                                        <span class="badge bg-light text-dark">{{ log.level }}</span>
                                    {% endif %}
                                </td>
                                <td>
                                    {% if log.action_type == 'recon' %}
                                        <span class="badge bg-success">侦察</span>
                                    {% elif log.action_type == 'analysis' %}
                                        <span class="badge bg-info">分析</span>
                                    {% elif log.action_type == 'decision' %}
                                        <span class="badge bg-warning text-dark">决策</span>
                                    {% elif log.action_type == 'execution' %}
                                        <span class="badge bg-danger">执行</span>
                                    {% elif log.action_type == 'login' %}
                                        <span class="badge bg-primary">登录</span>
                                    {% elif log.action_type == 'logout' %}
                                        <span class="badge bg-secondary">退出</span>
                                    {% else %}
                                        <span class="badge bg-light text-dark">{{ log.action_type or '系统' }}</span>
                                    {% endif %}
                                </td>
                                <td>{{ log.message }}</td>
                                <td>
                                    {% if log.user_id %}
                                        <span class="badge bg-light text-dark">用户{{ log.user_id }}</span>
                                    {% else %}
                                        <span class="text-muted">系统</span>
                                    {% endif %}
                                </td>
                            </tr>
                            {% endfor %}
                        </tbody>
                    </table>
                </div>

                <!-- 分页 -->
                {% if logs.pages > 1 %}
                <nav aria-label="日志分页">
                    <ul class="pagination justify-content-center">
                        {% if logs.has_prev %}
                            <li class="page-item">
                                <a class="page-link" href="{{ url_for('logs', page=logs.prev_num) }}">上一页</a>
                            </li>
                        {% endif %}
                        
                        {% for page_num in logs.iter_pages() %}
                            {% if page_num %}
                                {% if page_num != logs.page %}
                                    <li class="page-item">
                                        <a class="page-link" href="{{ url_for('logs', page=page_num) }}">{{ page_num }}</a>
                                    </li>
                                {% else %}
                                    <li class="page-item active">
                                        <span class="page-link">{{ page_num }}</span>
                                    </li>
                                {% endif %}
                            {% else %}
                                <li class="page-item disabled">
                                    <span class="page-link">...</span>
                                </li>
                            {% endif %}
                        {% endfor %}
                        
                        {% if logs.has_next %}
                            <li class="page-item">
                                <a class="page-link" href="{{ url_for('logs', page=logs.next_num) }}">下一页</a>
                            </li>
                        {% endif %}
                    </ul>
                </nav>
                {% endif %}
                {% else %}
                <div class="text-center py-5">
                    <i class="fas fa-list fa-3x text-muted mb-3"></i>
                    <h5 class="text-muted">暂无系统日志</h5>
                    <p class="text-muted">系统运行后会产生相应的日志记录</p>
                </div>
                {% endif %}
            </div>
        </div>
    </div>
</div>

<!-- 日志统计 -->
<div class="row mt-4">
    <div class="col-12">
        <div class="card">
            <div class="card-header">
                <h6 class="mb-0">
                    <i class="fas fa-chart-pie"></i> 日志统计
                </h6>
            </div>
            <div class="card-body">
                <div class="row text-center">
                    <div class="col-md-3">
                        <div class="mb-3">
                            <i class="fas fa-info-circle fa-2x text-info mb-2"></i>
                            <h4 class="text-info">
                                {{ logs.items|selectattr('level', 'equalto', 'info')|list|length }}
                            </h4>
                            <h6>信息日志</h6>
                        </div>
                    </div>
                    <div class="col-md-3">
                        <div class="mb-3">
                            <i class="fas fa-exclamation-triangle fa-2x text-warning mb-2"></i>
                            <h4 class="text-warning">
                                {{ logs.items|selectattr('level', 'equalto', 'warning')|list|length }}
                            </h4>
                            <h6>警告日志</h6>
                        </div>
                    </div>
                    <div class="col-md-3">
                        <div class="mb-3">
                            <i class="fas fa-times-circle fa-2x text-danger mb-2"></i>
                            <h4 class="text-danger">
                                {{ logs.items|selectattr('level', 'equalto', 'error')|list|length }}
                            </h4>
                            <h6>错误日志</h6>
                        </div>
                    </div>
                    <div class="col-md-3">
                        <div class="mb-3">
                            <i class="fas fa-bug fa-2x text-secondary mb-2"></i>
                            <h4 class="text-secondary">
                                {{ logs.items|selectattr('level', 'equalto', 'debug')|list|length }}
                            </h4>
                            <h6>调试日志</h6>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

<!-- 操作类型统计 -->
<div class="row mt-4">
    <div class="col-12">
        <div class="card">
            <div class="card-header">
                <h6 class="mb-0">
                    <i class="fas fa-chart-bar"></i> 操作类型统计
                </h6>
            </div>
            <div class="card-body">
                <div class="row text-center">
                    <div class="col-md-2">
                        <div class="mb-3">
                            <i class="fas fa-search fa-2x text-success mb-2"></i>
                            <h4 class="text-success">
                                {{ logs.items|selectattr('action_type', 'equalto', 'recon')|list|length }}
                            </h4>
                            <h6>侦察操作</h6>
                        </div>
                    </div>
                    <div class="col-md-2">
                        <div class="mb-3">
                            <i class="fas fa-chart-line fa-2x text-info mb-2"></i>
                            <h4 class="text-info">
                                {{ logs.items|selectattr('action_type', 'equalto', 'analysis')|list|length }}
                            </h4>
                            <h6>分析操作</h6>
                        </div>
                    </div>
                    <div class="col-md-2">
                        <div class="mb-3">
                            <i class="fas fa-gavel fa-2x text-warning mb-2"></i>
                            <h4 class="text-warning">
                                {{ logs.items|selectattr('action_type', 'equalto', 'decision')|list|length }}
                            </h4>
                            <h6>决策操作</h6>
                        </div>
                    </div>
                    <div class="col-md-2">
                        <div class="mb-3">
                            <i class="fas fa-cogs fa-2x text-danger mb-2"></i>
                            <h4 class="text-danger">
                                {{ logs.items|selectattr('action_type', 'equalto', 'execution')|list|length }}
                            </h4>
                            <h6>执行操作</h6>
                        </div>
                    </div>
                    <div class="col-md-2">
                        <div class="mb-3">
                            <i class="fas fa-sign-in-alt fa-2x text-primary mb-2"></i>
                            <h4 class="text-primary">
                                {{ logs.items|selectattr('action_type', 'equalto', 'login')|list|length }}
                            </h4>
                            <h6>登录操作</h6>
                        </div>
                    </div>
                    <div class="col-md-2">
                        <div class="mb-3">
                            <i class="fas fa-sign-out-alt fa-2x text-secondary mb-2"></i>
                            <h4 class="text-secondary">
                                {{ logs.items|selectattr('action_type', 'equalto', 'logout')|list|length }}
                            </h4>
                            <h6>退出操作</h6>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
{% endblock %}